//根据tags获取书籍信息
import { ref } from 'vue';

export const books = ref([]);
export const totalBooks = ref(0);

export const fetchBooks = async (category, proxy) => {
    try {
        const url = (category === 'all')
            ? '/api/book/all'
            : `/api/book/tag/${category}`;
        const response = await proxy.$http.get(url);

        // 检查响应的实际结构
        if (response.data && response.data.data && response.data.data.book) {
            books.value = response.data.data.book;
            totalBooks.value = books.value.length;
        }
        else {
            console.error('响应结构不符合预期:', response);
            books.value = [];
            totalBooks.value = 0;
        }

    } catch (error) {
        console.error('Error fetching books by tags:', error);
        books.value = [];
        totalBooks.value = 0;
    }
};